package com.project2.service;

import com.project2.bean.po.*;
import com.project2.bean.vo.MarketCategoryAndMarketGoods;
import com.project2.bean.vo.WxAllGoodsVo;
import com.project2.bean.vo.WxIndexChannel;
import com.project2.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：HsDsg
 * @date ：2022/8/14 17:40
 * @description：TODO
 */

@Service
public class WxHomeServiceImpl implements WxHomeService {

    @Autowired
    MarketBrandMapper brandMapper;
    @Autowired
    MarketAdMapper adMapper;
    @Autowired
    MarketCategoryMapper categoryMapper;
    @Autowired
    MarketCouponMapper couponMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketTopicMapper topicMapper;


    @Override
    public WxAllGoodsVo getAllGoods() {

        WxAllGoodsVo wxAllGoodsVo = new WxAllGoodsVo();
        //制造商
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketBrand> brandList = brandMapper.selectByExample(marketBrandExample);
        List<MarketBrand> marketBrandList = new ArrayList<>();
        for (MarketBrand marketBrand : brandList) {
            marketBrandList.add(marketBrand);
            if (marketBrandList.size() == 4) {
                break;
            }
        }

        //广告
        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria1 = marketAdExample.createCriteria();
        criteria1.andDeletedEqualTo(false);
        List<MarketAd> marketAds = adMapper.selectByExample(marketAdExample);

        //分类
//        MarketCategoryExample marketCategoryExample =new MarketCategoryExample();
//        MarketCategoryExample.Criteria criteria2 = marketCategoryExample.createCriteria();
//        criteria2.andDeletedEqualTo(false)
//                .andPidEqualTo(0);
//        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);
        List<WxIndexChannel> wxIndexChannels = categoryMapper.selectChannel();


        //优惠券
        MarketCouponExample marketCouponExample = new MarketCouponExample();
//        MarketAdExample.Criteria criteria3 = marketAdExample.createCriteria();
        MarketCouponExample.Criteria criteria3 = marketCouponExample.createCriteria();
        criteria3.andDeletedEqualTo(false)
                .andTypeEqualTo((short)0);
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(marketCouponExample);

        //商品分类以及商品
//        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
//        MarketGoodsExample.Criteria criteria4 = marketGoodsExample.createCriteria();
//        criteria4.andDeletedEqualTo(false);
//        List<MarketCategoryAndMarketGoods> categoryIdAndNames = categoryMapper.selectCategoryIdAndNames();
//        for (MarketCategoryAndMarketGoods categoryIdAndName : categoryIdAndNames) {
////            criteria4.andCategoryIdEqualTo(categoryIdAndName.getId());
////            List<MarketGoods> marketGoods = goodsMapper.selectByExample();
//            Integer categoryId = categoryIdAndName.getId();
//            List<MarketGoods> marketGoods = goodsMapper.selectGoodsByCategoryId(categoryId);
//            categoryIdAndName.setGoods(marketGoods);
//        }


        ArrayList<MarketCategoryAndMarketGoods> marketCategoryAndMarketGoods1 = new ArrayList<>();

        for (WxIndexChannel wxIndexChannel : wxIndexChannels) {
            Integer id = wxIndexChannel.getId();
            MarketCategoryAndMarketGoods marketCategoryAndMarketGoods = new MarketCategoryAndMarketGoods();
            List<Integer> categoryIdByCategoryId = categoryMapper.getCategoryIdByCategoryId(id);
            for (Integer integer : categoryIdByCategoryId) {

                List<MarketGoods> marketGoods = goodsMapper.selectGoodsByCategoryId(integer);

                for (int i =marketGoods.size()-1; i >5 ; i--) {
                    marketGoods.remove(i);
                }

                if (marketGoods.size()>0){
                    marketCategoryAndMarketGoods.setGoodsList(marketGoods);
                }

                marketCategoryAndMarketGoods.setId(wxIndexChannel.getId());
                marketCategoryAndMarketGoods.setName(wxIndexChannel.getName());
            }
            marketCategoryAndMarketGoods1.add(marketCategoryAndMarketGoods);
        }







//        for (MarketCategoryAndMarketGoods marketCategoryAndMarketGoods : marketCategoryAndMarketGoods1) {
////            List<MarketGoods> goodsList1 = marketCategoryAndMarketGoods.getGoodsList();
//            List<MarketGoods> goodsList = marketCategoryAndMarketGoods.getGoodsList();
//            for (WxIndexChannel wxIndexChannel : wxIndexChannels) {
//                List<Integer> categoryId = categoryMapper.getCategoryIdByCategoryId(wxIndexChannel.getId());
//                for (Integer integer : categoryId) {
//                    List<MarketGoods> marketGoods = goodsMapper.selectGoodsByCategoryId(integer);
////                    marketCategoryAndMarketGoods.setGoodsList(marketGoods);
//                    for (MarketGoods marketGood : marketGoods) {
//                        goodsList.add(marketGood);
//                    }
//                    marketCategoryAndMarketGoods.setName(wxIndexChannel.getName());
//                    marketCategoryAndMarketGoods.setId(wxIndexChannel.getId());
//                }
//            }
//        }
//        for (WxIndexChannel wxIndexChannel : wxIndexChannels) {
////            MarketCategoryAndMarketGoods marketCategoryAndMarketGoods = new MarketCategoryAndMarketGoods();
//            List<Integer> categoryId = categoryMapper.getCategoryIdByCategoryId(wxIndexChannel.getId());
////            List<MarketGoods> goodsList = marketCategoryAndMarketGoods.getGoodsList();
//            for (Integer integer : categoryId) {
//                ArrayList<MarketGoods> marketGoods1 = new ArrayList<>();
//                List<MarketGoods> marketGoods = goodsMapper.selectGoodsByCategoryId(integer);
//                goodsList=marketGoods;
////                marketCategoryAndMarketGoods.setGoodsList(marketGoods);
//                marketCategoryAndMarketGoods.setName(wxIndexChannel.getName());
//                marketCategoryAndMarketGoods.setId(wxIndexChannel.getId());
//                if (marketGoods.size()>0){
//                    continue;
//                }
//            }
//            List<MarketGoods> marketGoods = goodsMapper.selectGoodsByCategoryId(wxIndexChannel.getId());

//        marketCategoryAndMarketGoods1.add(marketCategoryAndMarketGoods);


        //热卖商品
        MarketGoodsExample marketGoodsExample2 = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria5 = marketGoodsExample2.createCriteria();
        criteria5.andIsHotEqualTo(true)
                 .andDeletedEqualTo(false);
        List<MarketGoods> marketHotGoods = goodsMapper.selectByExample(marketGoodsExample2);
        ArrayList<MarketGoods> marketGoods1 = new ArrayList<>();




        for (int i = marketHotGoods.size()-1; i >0 ; i--) {
            marketGoods1.add(marketHotGoods.get(i));
        }


        //新品
        MarketGoodsExample marketGoodsExample3 = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria6 = marketGoodsExample3.createCriteria();
        criteria6.andIsNewEqualTo(true)
                 .andDeletedEqualTo(false);
        List<MarketGoods> marketNewGoods = goodsMapper.selectByExample(marketGoodsExample3);
        ArrayList<MarketGoods> marketGoods = new ArrayList<>();
        for (int i = marketNewGoods.size()-1; i > 0; i--) {
            marketGoods.add(marketNewGoods.get(i));
        }


        //主题
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria7 = marketTopicExample.createCriteria();
        criteria7.andDeletedEqualTo(false);
        List<MarketTopic> marketTopics = topicMapper.selectByExample(marketTopicExample);

        wxAllGoodsVo.setBanner(marketAds);
//        wxAllGoodsVo.setBrandList(brandList);
        wxAllGoodsVo.setBrandList(marketBrandList);
        wxAllGoodsVo.setChannel(wxIndexChannels);
        wxAllGoodsVo.setCouponList(marketCoupons);
        wxAllGoodsVo.setFloorGoodsList(marketCategoryAndMarketGoods1);
        wxAllGoodsVo.setHotGoodsList(marketGoods1);
        wxAllGoodsVo.setNewGoodsList(marketGoods);
        wxAllGoodsVo.setTopicList(marketTopics);


        return wxAllGoodsVo;
    }


}
